package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.AlbumStatMapper;
import com.atguigu.tingshu.album.mapper.TrackStatMapper;
import com.atguigu.tingshu.album.service.MqOpService;
import com.atguigu.tingshu.album.strategy.ProcessDiffTypePurchases;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Slf4j
public class MqOpsServiceImpl implements MqOpService {
    @Autowired
    private AlbumStatMapper albumStatMapper;
    @Autowired
    private TrackStatMapper trackStatMapper;
    @Autowired
    private Map<String, ProcessDiffTypePurchases> map;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void listenAlbumAndTrackStatNumUpdate(TrackStatMqVo trackStatMqVo) {
        // param1  更新多少 param2:更新专辑 param3:更新的维度
        Long albumId = trackStatMqVo.getAlbumId();
        String statType = trackStatMqVo.getStatType();
        Integer count = trackStatMqVo.getCount();
        Long trackId = trackStatMqVo.getTrackId();
        try {
            if (albumId != null) {
                // 播放量
                int i = albumStatMapper.updateAlbumStatNum(albumId, statType, count);
                log.info("修改专辑播放量：{}", i > 1 ? "success" : "fail");

                int row = trackStatMapper.updateTrackStatNum(trackId, SystemConstant.TRACK_STAT_PLAY, count);
                log.info("修改声音播放量：{}", row > 1 ? "success" : "fail");
            } else {
                //收藏量（0702）
                int row = trackStatMapper.updateTrackStatNum(trackId, statType, count);
                log.info("修改声音收藏量：{}", row > 1 ? "success" : "fail");
            }
        } catch (Exception e) {
            throw new GuiguException(201, "数据库操作失败");
        }

    }

    @Override
    public void updateAlbumAndBuyNum(OrderInfo orderInfo) {
        map.get(orderInfo.getItemType()).processTypePurchases(orderInfo);
    }
}
